using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class SiteEvent
	{
		#region Constants

		private const string TableName = "[dbo].[SiteEvent]";

		#endregion
	
		#region Properties

		public int? EventId { get; set; }
		public string Title { get; set; }
		public string EventPlace { get; set; }
		public string EventContent { get; set; }
		public int? EventTypeCode { get; set; }
		public int? MembersCount { get; set; }
		public DateTime? StartDate { get; set; }
		public DateTime? FinishDate { get; set; }
		public bool? IsDeleted { get; set; }
		public bool? IsVisible { get; set; }
		public DateTime? CreateDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }

		#endregion
	
		#region Methods
	
		#region Mapping Methods
	
		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);
		
			ds.Tables[TableName].Columns.Add("EventId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("Title", typeof(String));
			ds.Tables[TableName].Columns.Add("EventPlace", typeof(String));
			ds.Tables[TableName].Columns.Add("EventContent", typeof(String));
			ds.Tables[TableName].Columns.Add("EventTypeCode", typeof(Int32));
			ds.Tables[TableName].Columns.Add("MembersCount", typeof(Int32));
			ds.Tables[TableName].Columns.Add("StartDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("FinishDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("IsVisible", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("LastModifiedDate", typeof(DateTime));
		
			DataRow dr = ds.Tables[TableName].NewRow();

			dr["EventId"] = EventId ?? (object) DBNull.Value;
			dr["Title"] = Title ?? (object) DBNull.Value;
			dr["EventPlace"] = EventPlace ?? (object) DBNull.Value;
			dr["EventContent"] = EventContent ?? (object) DBNull.Value;
			dr["EventTypeCode"] = EventTypeCode ?? (object) DBNull.Value;
			dr["MembersCount"] = MembersCount ?? (object) DBNull.Value;
			dr["StartDate"] = StartDate ?? (object) DBNull.Value;
			dr["FinishDate"] = FinishDate ?? (object) DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object) DBNull.Value;
			dr["IsVisible"] = IsVisible ?? (object) DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object) DBNull.Value;
			dr["LastModifiedDate"] = LastModifiedDate ?? (object) DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}
	
		protected void MapFrom(DataRow dr)
		{
			EventId = dr["EventId"] != DBNull.Value ? Convert.ToInt32(dr["EventId"]) : EventId = null;
			Title = dr["Title"] != DBNull.Value ? Convert.ToString(dr["Title"]) : Title = null;
			EventPlace = dr["EventPlace"] != DBNull.Value ? Convert.ToString(dr["EventPlace"]) : EventPlace = null;
			EventContent = dr["EventContent"] != DBNull.Value ? Convert.ToString(dr["EventContent"]) : EventContent = null;
			EventTypeCode = dr["EventTypeCode"] != DBNull.Value ? Convert.ToInt32(dr["EventTypeCode"]) : EventTypeCode = null;
			MembersCount = dr["MembersCount"] != DBNull.Value ? Convert.ToInt32(dr["MembersCount"]) : MembersCount = null;
			StartDate = dr["StartDate"] != DBNull.Value ? Convert.ToDateTime(dr["StartDate"]) : StartDate = null;
			FinishDate = dr["FinishDate"] != DBNull.Value ? Convert.ToDateTime(dr["FinishDate"]) : FinishDate = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
			IsVisible = dr["IsVisible"] != DBNull.Value ? Convert.ToBoolean(dr["IsVisible"]) : IsVisible = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			LastModifiedDate = dr["LastModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr["LastModifiedDate"]) : LastModifiedDate = null;
		}

		public static List<SiteEvent> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<SiteEvent> objects = new List<SiteEvent>();
		
			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;
		
			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[SiteEvent] in DataSet.");
		
			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[SiteEvent] is empty.");
		
			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				SiteEvent instance = new SiteEvent();
				instance.MapFrom(dr);
				objects.Add(instance);
			}
		
			// Return collection.
			return objects;
		}
	
		#endregion
	
		#region CRUD Methods
	
		public static SiteEvent Get(Int32 eventId)
		{
			SiteEvent instance = new SiteEvent();
		
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, eventId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get SiteEvent ID:{0} from Database.", eventId));
					ds.Tables[0].TableName = TableName;
		
					instance.MapFrom( ds.Tables[0].Rows[0] );
				}
			}
			return instance;
		}
	
		#region INSERT
		private void Insert(String title, String eventPlace, String eventContent, Int32? eventTypeCode, Int32? membersCount, DateTime? startDate, DateTime? finishDate, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, title, eventPlace, eventContent, eventTypeCode, membersCount, startDate, finishDate, isDeleted, isVisible, createDate, lastModifiedDate))
			{
				EventId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}

		/// <summary>
		/// Insert current SiteEvent to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(Title, EventPlace, EventContent, EventTypeCode, MembersCount, StartDate, FinishDate, IsDeleted, IsVisible, CreateDate, LastModifiedDate, transaction);
		}

		#endregion
	
		#region UPDATE

		private static void Update(Int32? eventId, String title, String eventPlace, String eventContent, Int32? eventTypeCode, Int32? membersCount, DateTime? startDate, DateTime? finishDate, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@eventId"].Value = eventId;
				dbCommand.Parameters["@title"].Value = title;
				dbCommand.Parameters["@eventPlace"].Value = eventPlace;
				dbCommand.Parameters["@eventContent"].Value = eventContent;
				dbCommand.Parameters["@eventTypeCode"].Value = eventTypeCode;
				dbCommand.Parameters["@membersCount"].Value = membersCount;
				dbCommand.Parameters["@startDate"].Value = startDate;
				dbCommand.Parameters["@finishDate"].Value = finishDate;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
				dbCommand.Parameters["@isVisible"].Value = isVisible;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@lastModifiedDate"].Value = lastModifiedDate;
		
				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

		private static void Update(Int32? eventId, String title, String eventPlace, String eventContent, Int32? eventTypeCode, Int32? membersCount, DateTime? startDate, DateTime? finishDate, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Update(eventId, title, eventPlace, eventContent, eventTypeCode, membersCount, startDate, finishDate, isDeleted, isVisible, createDate, lastModifiedDate, null);
		}

		private static void Update(SiteEvent siteEvent)
		{
			siteEvent.Update();
		}

		private static void Update(SiteEvent siteEvent, DbTransaction transaction)
		{
			siteEvent.Update(transaction);
		}
	
		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@eventId"].SourceColumn = "EventId";
				dbCommand.Parameters["@title"].SourceColumn = "Title";
				dbCommand.Parameters["@eventPlace"].SourceColumn = "EventPlace";
				dbCommand.Parameters["@eventContent"].SourceColumn = "EventContent";
				dbCommand.Parameters["@eventTypeCode"].SourceColumn = "EventTypeCode";
				dbCommand.Parameters["@membersCount"].SourceColumn = "MembersCount";
				dbCommand.Parameters["@startDate"].SourceColumn = "StartDate";
				dbCommand.Parameters["@finishDate"].SourceColumn = "FinishDate";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";
				dbCommand.Parameters["@isVisible"].SourceColumn = "IsVisible";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@lastModifiedDate"].SourceColumn = "LastModifiedDate";

				using (DataSet ds = new DataSet())
				{
					MapTo( ds );
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}
	
		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		private void Update()
		{
			Update((DbTransaction)null);
		}
		
		#endregion
	
		#region DELETE

		public static void Delete(Int32? eventId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, eventId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}
	
		public static void Delete(Int32? eventId)
		{
			Delete(
				eventId);
		}
	
		/// <summary>
		/// Delete current SiteEvent from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, EventId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			EventId = null;
		}
	
		/// <summary>
		/// Delete current SiteEvent from database.
		/// </summary>
		public void Delete()
		{
			Delete((DbTransaction)null);
		}
	
		#endregion
	
		#region SEARCH

		private static List<SiteEvent> Search(Int32? eventId, String title, String eventPlace, String eventContent, Int32? eventTypeCode, Int32? membersCount, DateTime? startDate, DateTime? finishDate, Boolean? isDeleted, Boolean? isVisible, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspSiteEvent_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, eventId, title, eventPlace, eventContent, eventTypeCode, membersCount, startDate, finishDate, isDeleted, isVisible, createDate, lastModifiedDate))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		private static List<SiteEvent> Search(SiteEvent searchObject)
		{
			return Search (searchObject.EventId, searchObject.Title, searchObject.EventPlace, searchObject.EventContent, searchObject.EventTypeCode, searchObject.MembersCount, searchObject.StartDate, searchObject.FinishDate, searchObject.IsDeleted, searchObject.IsVisible, searchObject.CreateDate, searchObject.LastModifiedDate);
		}
	
		#endregion
	
		#endregion
	
		#endregion
	}
}